var gulp = require('gulp'),
    concat = require('gulp-concat'),
    cssmin = require('gulp-minify-css'),
    jsmin = require('gulp-jsmin'),
    imgmin = require('gulp-image'),
    htmlmin = require('gulp-htmlmin'),
    cache = require('gulp-cache'),
    connect = require('gulp-connect'),
    watch = require('gulp-watch'),
    open = require('gulp-open'),
    htmlreplace = require('gulp-html-replace'),
    zip = require('gulp-zip');

var port = 3000;
var path = {
    assets: './source',
    public: './public',
    bower_components: './bower_components'
};

gulp.task('html', function () {
    return gulp.src(path.assets + '/htmls/**/*.html')
        .pipe(htmlmin({
            collapseWhitespace: true,
            removeComments: true
            //removeScriptTypeAttributes: true,
            //collapseBooleanAttributes: true

        }))
        .pipe(gulp.dest(path.public + "/templates"));
});

gulp.task('img', function () {
    return gulp.src(path.assets + '/images/**/*')
        .pipe(cache(imgmin()))
        .pipe(gulp.dest(path.public + "/images"));
});

gulp.task('css', function () {
    return gulp.src(path.assets + '/styles/**/*.css')
        .pipe(concat('app.css'))
        .pipe(cssmin())
        .pipe(gulp.dest(path.public + "/css"));
});

gulp.task('replace', function () {
    return gulp.src(path.public + '/index.html')
        .pipe(htmlreplace({
            css: './css/app.css?v=' + new Date().getTime(),
            js: './js/app.js?v=' + new Date().getTime()
        }, {
            keepUnassigned: true,
            keepBlockTags: true
        }))
        .pipe(gulp.dest(path.public));
});

gulp.task('js', function () {
    return gulp.src([
        path.assets + '/scripts/**/*.js'
    ])
        .pipe(concat('app.js'))
        .pipe(jsmin())
        .pipe(gulp.dest(path.public + "/js"));
});

gulp.task('reload-css', ['css'], function () {
    return gulp.src(path.public + "/css/app.css")
        .pipe(connect.reload());
});

gulp.task('reload-js', ['js'], function () {
    return gulp.src(path.public + "/js/app.js")
        .pipe(connect.reload());
});

gulp.task('reload-html', function () {
    return gulp.src(path.public + "/**/*.html")
        .pipe(connect.reload());
});

gulp.task('reload-img', ['img'], function () {
    return gulp.src(path.public + "/**/*")
        .pipe(connect.reload());
});


gulp.task('start-server', function () {
    return connect.server({
        root: 'public',
        port: port,
        livereload: true
    });
});

gulp.task('open-browser', function () {
    return gulp.src(__filename)
        .pipe(open({
            uri: 'http://localhost:' + port
        }));
});

gulp.task('zip', ['default'], function () {
    return gulp.src(path.public)
        .pipe(zip('public.zip'))
        .pipe(gulp.dest('./'));
});

gulp.task('watch', ['default'], function () {
    gulp.watch(path.assets + "/images/**/*", ['reload-img']);
    gulp.watch(path.assets + "/styles/**/*", ['reload-css', 'replace']);
    gulp.watch(path.assets + "/scripts/**/*", ['reload-js', 'replace']);
    gulp.watch(path.assets + "/templates/**/*", ['html']);
    gulp.watch(path.public + "/**/*.html", ['reload-html']);
});

gulp.task('clear-cache', function (done) {
    return cache.clearAll(done);
});

gulp.task('default', ['html','img', 'css', 'js', 'replace']);
gulp.task('serve', ['default', 'start-server', 'open-browser', 'watch']);
